package cn.bigcontainer.alt.meta.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.meta.dto.MCrud;
import cn.bigcontainer.alt.meta.service.MetaManService;
import cn.bigcontainer.alt.meta.service.MetaService;

/**
 * 元数据管理页面
 */
@Controller
@RequestMapping("/meta/man")
public class MetaController {

    @Resource
    private MetaService metaService;

    /**
     * 所有对象展示
     * @param app
     * @param req
     * @param map
     * @return
     */
    @RequestMapping("")
    public String toMain(MCrud app, @RequestParam Map<String, Object> req, Map<String, Object> map) {
        map.put("objects", metaService.getObjects());
        return "meta/man/index";
    }

    /**
     * 单个对象元数据管理页面
     * @param objectid
     * @param map
     * @return
     */
    @RequestMapping("/object")
    public String loadMeta(String objectid, Map<String, Object> map) {
        map.put("object", metaService.getObjectWithApps(objectid));
        return "meta/man/object";
    }

    /**
     * 根据视图ID、视图属性ID或者按钮ID来加载元数据管理页面
     * @param view
     * @param viewproperty
     * @param viewbutton
     * @param map
     * @return
     */
    @RequestMapping("/view")
    public String loadMeta(String view, String viewproperty, String viewbutton, Map<String, Object> map) {
        map.put("object", metaService.getObjectWithApps(view, viewproperty, viewbutton));
        return "meta/man/object";
    }

    @Resource
    private MetaManService metaManService;

    @RequestMapping("/saveMObject")
    @ResponseBody
    public BigResultCode saveMObject (String objectId, String id, String property, String value ) {
        metaManService.saveMObject(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/deleteMObject")
    @ResponseBody
    public BigResultCode deleteMObject(String objectId, String id, String property, String value){
        metaManService.deleteMObject(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMObject")
    @ResponseBody
    public BigResultCode addMObject(String objectId, String id, String property, String value){
        metaManService.addMObject(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMPropertyOrMRelation")
    @ResponseBody
    public BigResultCode addMPropertyOrMRelation (String objectId, String prop, String propConfig) {
    	if (propConfig.startsWith("关联:")) {
    		metaService.createRelationMeta(objectId, prop, propConfig);
    	} else {
    		metaService.createPropertyMeta(objectId, prop, propConfig);
    	}
    	return BigResultCode.getSuccessCode();
    }
    
    @RequestMapping("/saveMProperty")
    @ResponseBody
    public BigResultCode saveMProperty (String objectId, String id, String property, String value ) {
        metaManService.saveMProperty(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/deleteMProperty")
    @ResponseBody
    public BigResultCode deleteMProperty(String objectId, String id, String property, String value){
        metaManService.deleteMProperty(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMProperty")
    @ResponseBody
    public BigResultCode addMProperty(String objectId, String id, String property, String value){
        metaManService.addMProperty(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/saveMRelation")
    @ResponseBody
    public BigResultCode saveMRelation (String objectId, String id, String property, String value ) {
        metaManService.saveMRelation(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/deleteMRelation")
    @ResponseBody
    public BigResultCode deleteMRelation(String objectId, String id, String property, String value){
        metaManService.deleteMRelation(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMRelation")
    @ResponseBody
    public BigResultCode addMRelation(String objectId, String id, String property, String value){
        metaManService.addMRelation(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/saveMApp")
    @ResponseBody
    public BigResultCode saveMApp (String objectId, String id, String property, String value ) {
        metaManService.saveMApp(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/deleteMApp")
    @ResponseBody
    public BigResultCode deleteMApp(String objectId, String id, String property, String value){
        metaManService.deleteMApp(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMApp")
    @ResponseBody
    public BigResultCode addMApp(String objectId, String id, String property, String value){
        metaManService.addMApp(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/saveMView")
    @ResponseBody
    public BigResultCode saveMView (String objectId, String id, String property, String value ) {
        metaManService.saveMView(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/deleteMView")
    @ResponseBody
    public BigResultCode deleteMView(String objectId, String id, String property, String value){
        metaManService.deleteMView(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMView")
    @ResponseBody
    public BigResultCode addMView(String objectId, String id, String property, String value){
        metaManService.addMView(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }
    
    /**
     * 通过属性创建视图属性
     * @param viewId
     * @param propertyId
     * @param viewPropertyType
     * @param viewPropertyConfig
     * @return
     */
    @RequestMapping("/addMViewPropertyByMProperty")
    @ResponseBody
    public BigResultCode addMViewPropertyByMProperty (String viewId, String propertyId, String viewPropertyType, String viewPropertyConfig) {
    	this.metaManService.addMViewPropertyByMProperty(viewId, propertyId, viewPropertyType, viewPropertyConfig);
        return BigResultCode.getSuccessCode();
    }
    
    /**
     * 通过关系创建视图属性
     * @param viewId
     * @param relationId
     * @param viewPropertyType
     * @param viewPropertyConfig
     * @return
     */
    @RequestMapping("/addMViewPropertyByMRelation")
    @ResponseBody
    public BigResultCode addMViewPropertyByMRelation (String viewId, String relationId, String viewPropertyType, String viewPropertyConfig) {
        this.metaManService.addMViewPropertyByMRelation(viewId, relationId, viewPropertyType, viewPropertyConfig);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/saveMViewProperty")
    @ResponseBody
    public BigResultCode saveMViewProperty (String objectId, String id, String property, String value ) {
        metaManService.saveMViewProperty(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/deleteMViewProperty")
    @ResponseBody
    public BigResultCode deleteMViewProperty(String objectId, String id, String property, String value){
        metaManService.deleteMViewProperty(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMViewProperty")
    @ResponseBody
    public BigResultCode addMViewProperty(String objectId, String id, String property, String value){
        metaManService.addMViewProperty(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/saveMButton")
    @ResponseBody
    public BigResultCode saveMButton (String objectId, String id, String property, String value ) {
        metaManService.saveMButton(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/deleteMButton")
    @ResponseBody
    public BigResultCode deleteMButton(String objectId, String id, String property, String value){
        metaManService.deleteMButton(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/addMButton")
    @ResponseBody
    public BigResultCode addMButton(String objectId, String id, String property, String value){
        metaManService.addMButton(objectId, id, property, value);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/setMeta")
    @ResponseBody
    public BigResultCode setMeta(String id){
        metaManService.setMeta(id);
        return BigResultCode.getSuccessCode();
    }
    
    @RequestMapping("/setMetaAll")
    @ResponseBody
    public BigResultCode setMetaAll(){
        metaManService.setMetaAll();
        return BigResultCode.getSuccessCode();
    }
    
    @RequestMapping("/unsetMetaAll")
    @ResponseBody
    public BigResultCode unsetMetaAll(){
        metaManService.unsetMetaAll();
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/object/delete")
    @ResponseBody
    public BigResultCode deleteObject(String id){
        metaManService.deleteObject(id);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/app/delete")
    @ResponseBody
    public BigResultCode deleteApp(String id){
        metaManService.deleteApp(id);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/app/copy")
    @ResponseBody
    public BigResultCode copyApp(String id){
        metaManService.copyApp(id);
        return BigResultCode.getSuccessCode();
    }

    @RequestMapping("/app/createLeditor")
    @ResponseBody
    public BigResultCode createLeditor(String id){
        metaService.createEditorByCrud(id);
        return BigResultCode.getSuccessCode();
    }
    
}
